﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace Cache
{

    /// <summary>
    /// 缓存接口
    /// </summary>
    public interface ICache
    {
        /// <summary>
        /// 缓存配置
        /// </summary>
        CacheConfig CacheConfig { get; set; }

        /// <summary>
        /// 当前缓存的集合
        /// </summary>
        List<object> KeyList { get; set; }

        /// <summary>
        /// 初始化实例
        /// </summary>
        void Init();

        /// <summary>
        /// 设置泛型缓存项，如该项已存在则覆盖原有值（自动序列化为JSON格式）
        /// </summary>
        /// <param name="key">缓存key值</param>
        /// <param name="value">缓存value值</param>
        /// <param name="expirationTime">过期时间</param>
        /// <returns>对象</returns>
        void Set(object key, object value, int? expirationTime = null);

        /// <summary>
        /// 获取泛型缓存项
        /// </summary>
        /// <param name="key">缓存key值</param>
        /// <returns>返回缓存泛型实例对象，不存在的key则返回null</returns>
        string Get(object key, int? expirationTime = null);

        string GetValue(string key);

        /// <summary>
        /// 获取泛型缓存项
        /// </summary>
        /// <param name="key">缓存key值</param>
        /// <param name="func">执行方法</param>
        /// <param name="expirationTime">过期时间</param>
        /// <returns>返回缓存泛型实例对象，不存在的key则返回null</returns>
        T Get<T>(object key, Func<T> func = null, int? expirationTime = null);

        /// <summary>
        /// 获得全部key
        /// </summary>
        /// <returns></returns>
        public List<string> Keys();

        /// <summary>
        /// 移除指定缓存
        /// </summary>
        /// <param name="key">缓存key值</param>
        void Remove(object key);

        /// <summary>
        /// 移除所有缓存
        /// </summary>
        void RemoveAll();

        /// <summary>
        /// 获取泛型缓存项
        /// </summary>
        /// <param name="key">缓存key值</param>
        /// <param name="func">执行方法</param>
        /// <param name="expirationTime">过期时间</param>
        /// <returns>返回缓存泛型实例对象，不存在的key则返回null</returns>
        dynamic Get(object key, Func<object> func, int? expirationTime);

        /// <summary>
        /// 将一个或多个值插入到列表头部
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="key">缓存key</param>
        /// <param name="obj">成员</param>
        /// <returns>插入的个数</returns>
        long LPush<T>(string key, T obj);

        /// <summary>
        /// 移除并获取列表的第一个元素
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="key">缓存key</param>
        /// <returns>列表的第一个元素</returns>
        T LPop<T>(string key);

        /// <summary>
        /// 在列表中添加一个或多个值
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="key">缓存key</param>
        /// <param name="obj">成员</param>
        /// <returns>添加的个数</returns>
        long RPush<T>(string key, T obj);

        /// <summary>
        /// 移除并获取列表的最后一个元素
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="key">缓存key</param>
        /// <returns>列表的最后一个元素</returns>
        T RPop<T>(string key);

        /// <summary>
        /// 获取指定返回内的集合
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="key">缓存key</param>
        /// <param name="start">开始索引</param>
        /// <param name="stop">结束索引</param>
        /// <returns>指定返回内的集合</returns>
        T[] LRange<T>(string key, int start, int stop);

        /// <summary>
        /// 向集合中添加一个或多个成员
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="obj">成员</param>
        /// <returns>添加个数</returns>
        long SAdd<T>(string key, T obj);

        /// <summary>
        /// 返回成员集合
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns>成员集合</returns>
        T[] SMembers<T>(string key);

        /// <summary>
        /// 返回成员集合
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns>成员集合</returns>
        string[] SMembers(string key);

        /// <summary>
        /// 移除集合中的一个或多个成员
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="key">缓存key</param>
        /// <param name="members">成员集合</param>
        /// <returns>移除的个数</returns>
        long SRem<T>(string key, T[] members);

        /// <summary>
        /// 移除集合中的一个或多个成员
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="key">缓存key</param>
        /// <param name="members">成员集合</param>
        /// <returns>移除的个数</returns>
        long SRem(string key, string[] members);

        /// <summary>
        /// 是否存在缓存key
        /// </summary>
        /// <param name="key">缓存key</param>
        /// <returns>是否存在</returns>
        bool Exists(string key);

        bool Lock(string key, int lockExpirySeconds = 10, double waitLockSeconds = 0);

        bool DelLock(string key);
    }
}
